package com.hfjyz.base.dao;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.hfjyz.base.entity.BaseEntity;

public class BaseDao<T extends BaseEntity> {

    protected HibernateTemplate hibernateTemplate;
    private Class<?> clazz = null;

    public BaseDao(Class<?> clazz) {
        this.clazz = clazz;
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @SuppressWarnings("unchecked")
    public T get(Integer id) {
        return (T) hibernateTemplate.get(clazz, id);
    }

    public void delete(Integer id) {
        Object entity = hibernateTemplate.get(clazz, id);
        hibernateTemplate.delete(entity);
    }

    public void saveOrUpdate(T entity) {
        hibernateTemplate.saveOrUpdate(entity);
    }
    
    @SuppressWarnings("unchecked")
    public List<T> findAll() {
        String hql = "from " + clazz.getSimpleName() + " order by id desc";
        return (List<T>) hibernateTemplate.find(hql);
    }
}
